package com.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.beans.AdminInfo;

public class SessionFilter implements Filter {
	public void destroy() {
	}
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req=(HttpServletRequest)request;
		String uri=req.getRequestURI();
		String contextPath=req.getContextPath();
		HttpSession session =req.getSession();
		if(uri.contains("login.html")|| uri.equals(contextPath+"/") ) {
			chain.doFilter(req,response);
		}
		else {
			AdminInfo admin=(AdminInfo)session.getAttribute("session_admin");
			if(admin==null) {
				response.getWriter().print("<script>window.top.location.href='login.html'</script>");
			}
			else {
				chain.doFilter(req,response);
			}
		}
	}
	public void init(FilterConfig fConfig) throws ServletException {
	}

}
